using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Framework;
using System.IO;

namespace BizTalk.BuildGenerator.Tasks.BAM
{
    /// <summary>
    /// Backs up all definitions
    /// </summary>
    public class BackupAllDefinitions : BaseBMTask 
    {
        private string _OutputFilePath;
        private bool _OverwriteExistingFile = true;

        /// <summary>
        /// Default true, but determines if an existing file will be replaced
        /// </summary>
        public bool OverwriteExistingFile
        {
            get { return _OverwriteExistingFile; }
            set { _OverwriteExistingFile = value; }
        }
        /// <summary>
        /// The path to the file where this will be placed
        /// </summary>
        [Required]
        public string OutputFilePath
        {
            get { return _OutputFilePath; }
            set { _OutputFilePath = value; }
        }            
        /// <summary>
        /// Executes the task
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            if (this._OverwriteExistingFile && File.Exists(this._OutputFilePath))
                File.Delete(this._OutputFilePath);

            const string argsFormat = "get-defxml -FileName:{0}";
            return base.ExecuteBM(string.Format(argsFormat, this._OutputFilePath));            
        }
    }
}
